package tech.mobi.child.setting;

import tech.mobi.child.MainAct;
import tech.mobi.child.PortalAct;
import tech.mobi.child.base.BaseAct;
import tech.mobi.child.service.MainService;
import tech.mobi.child.utils.BaiduPushUtils;
import tech.mobi.child.R;

import com.baidu.android.pushservice.CustomPushNotificationBuilder;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;

import android.app.Activity;
import android.app.Notification;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * 自定义的显示通知的Activity
 * 
 */
public class PushCustomActivity1 extends BaseAct implements OnClickListener {

	private TextView tvTitle;
	private TextView tvContent;
	private Button btnClose;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Log.i("MyLog", "ACT1_ss2");
		try {
			Intent intent = getIntent();
			if (intent == null) {
				Log.i("MyLog", "ACT1_ss22222");
			} else {
				Log.i("MyLog", "ACT1_ss33333");
				String title = intent
						.getStringExtra(PushConstants.EXTRA_NOTIFICATION_TITLE);
				String content = intent
						.getStringExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT);
				if (title == null) {
					Log.i("MyLog", "ACT1_title:" + title);
					if (MainAct.mainUI != null) {
						Log.i("MyLog", "ACT1_title:" + title + ";MainAct.mainUI != null;");
						this.finish();
					} else {
						Log.i("MyLog", "ACT1_title:" + title + ";MainAct.mainUI == null;");
						
						for (BaseAct baseAct : MainService.allActivity) {
							if (!"PushCustomActivity1".toLowerCase().equals(
									baseAct.getClass().getName().toLowerCase())) {

								baseAct.finish();
							}
						}
						
						// baidu-Push 开始
						// 以apikey的方式登录，一般放在主Activity的onCreate中
						PushManager.startWork(getApplicationContext(),
								PushConstants.LOGIN_TYPE_API_KEY, BaiduPushUtils.getMetaValue(
										PushCustomActivity1.this, "BaiduPush_channel_api_key"));

						 Resources resource = this.getResources();
						 String pkgName = this.getPackageName();
						
						 //设置自定义的通知样式，如果想使用系统默认的可以不加这段代码
						 CustomPushNotificationBuilder cBuilder = new
						 CustomPushNotificationBuilder(
						 resource.getIdentifier("notification_custom_builder", "layout",
						 pkgName),
						 resource.getIdentifier("notification_icon", "id", pkgName),
						 resource.getIdentifier("notification_title", "id", pkgName),
						 resource.getIdentifier("notification_text", "id", pkgName));
						 cBuilder.setNotificationFlags(Notification.FLAG_AUTO_CANCEL);
						 cBuilder.setNotificationDefaults(Notification.DEFAULT_SOUND |
						 Notification.DEFAULT_VIBRATE);
						 cBuilder.setStatusbarIcon(this.getApplicationInfo().icon);
						 cBuilder.setLayoutDrawable(resource.getIdentifier("simple_notification_icon",
						 "drawable", pkgName));
						 PushManager.setNotificationBuilder(this, 1, cBuilder);
						// baidu-Push 结束
						
						Intent intent2 = new Intent(PushCustomActivity1.this,
								PortalAct.class);
						PushCustomActivity1.this.startActivity(intent2);
						//PushCustomActivity.this.finish();
					}
				} else {
					setContentView(R.layout.push_custom_activity);
					Log.i("MyLog", "ACT1_title:" + title + ";title != null;");
					
					tvTitle = (TextView) this.findViewById(R.id.tvTitle);
					tvContent = (TextView) this.findViewById(R.id.tvContent);

					btnClose = (Button) this.findViewById(R.id.btnClose);
					btnClose.setOnClickListener(this);
					Log.i("MyLog", "ACT1_title:" + title);
					tvTitle.setText(title);
					tvContent.setText(content);
				}
			}
		} catch (Exception ex) {
			Log.i("MyLog", "ACT1_Exception:" + ex.getMessage());
			
		}

		

		// Resources resource = this.getResources();
		// String pkgName = this.getPackageName();
		//
		// setContentView(resource.getIdentifier("custom_activity", "layout",
		// pkgName));
		//
		// TextView titleView = (TextView)
		// this.findViewById(resource.getIdentifier("title", "id", pkgName));
		// TextView contentView = (TextView)
		// this.findViewById(resource.getIdentifier("content", "id", pkgName));
		//
		// Intent intent = getIntent();
		// String title =
		// intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_TITLE);
		// String content =
		// intent.getStringExtra(PushConstants.EXTRA_NOTIFICATION_CONTENT);
		//
		// titleView.setText(title);
		// contentView.setText(content);

	}

	// @Override
	// public void init() {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// public void refresh(Object... param) {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// protected boolean isRouteDisplayed() {
	// // TODO Auto-generated method stub
	// return false;
	// }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnClose:
//			if (MainAct.mainUI != null) {
//				MainAct.mainUI.finish();
//				for (BaseAct baseAct : MainService.allActivity) {
//					if (!"PushCustomActivity".toLowerCase().equals(
//							baseAct.getClass().getName().toLowerCase())) {
//
//						baseAct.finish();
//					}
//				}
//			}
//			Intent intent = new Intent(PushCustomActivity.this, PortalAct.class);
//			PushCustomActivity.this.startActivity(intent);
			PushCustomActivity1.this.finish();
			break;
		}

	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void refresh(Object... param) {
		// TODO Auto-generated method stub

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	// @Override
	// protected void onStart() {
	// super.onStart();
	// PushManager.activityStarted(this);
	// }
}
